//
//  SNKNewsCell.m
//  SleepNews
//
//  Created by Kavee DJ on 2016/12/10.
//  Copyright © 2016年 Kavee DJ. All rights reserved.
//

#import "SNKNewsCell.h"
#import "SNKDataModel.h"
#import "NSString+KDJExtension.h"

#import <UIImageView+WebCache.h>
#import <NSDate+DateTools.h>

@implementation SNKNewsCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"newscell";
    SNKNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[SNKNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 添加imageV
        UIImageView *imageV = [[UIImageView alloc] init];
        imageV.frame = CGRectMake(8, 8, 80, 60);
        [self addSubview:imageV];
        self.imgIcon = imageV;
        
        // 添加label
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageV.frame)+10, 10, SCREENW-CGRectGetMaxX(imageV.frame)-20, 40)];
        label.numberOfLines = 0;
        if (SCREENW == 320) {
            label.font = [UIFont systemFontOfSize:15];
        } else {
            label.font = [UIFont systemFontOfSize:16];
        }
        [self addSubview:label];
        self.lblTitle = label;
        
        UILabel *scrL = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageV.frame)+10, CGRectGetMaxY(label.frame), SCREENW-CGRectGetMaxX(imageV.frame)-20, 40)];
        scrL.numberOfLines = 0;
        scrL.font = [UIFont systemFontOfSize:14];
        scrL.textColor = [UIColor lightGrayColor];
        [self addSubview:scrL];
        self.lblSubtitle = scrL;
        
        CGFloat x = SCREENW-5-100;
        CGFloat y = CGRectGetMaxY(imageV.frame)-10;
        CGFloat w = 100;
        CGFloat h = 15;
        UILabel *replyL = [[UILabel alloc]init];
        replyL.frame = CGRectMake(x, y, w, h);
        replyL.textAlignment = NSTextAlignmentCenter;
        replyL.font = [UIFont systemFontOfSize:10];
        replyL.textColor = [UIColor darkGrayColor];
        [self addSubview:replyL];
        self.lblReply = replyL;
        
        CGFloat resorceX = CGRectGetMaxX(imageV.frame)+10;
        CGFloat resorceY = CGRectGetMaxY(imageV.frame) - 10;
        CGFloat resorceW = 150;
        CGFloat resorceH = 20;
        UILabel *resorceL = [[UILabel alloc]init];
        resorceL.font = [UIFont systemFontOfSize:10];
        resorceL.textColor = [UIColor darkGrayColor];
        resorceL.frame = CGRectMake(resorceX, resorceY, resorceW, resorceH);
        [self addSubview:resorceL];
        self.resorceL = resorceL;
        
//        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 79, SCREENW - 20, 1)];
//        line.backgroundColor = HEXColor(@"eeeeee");
//        [self addSubview:line];
    }
    return self;
}

- (void)setDataModel:(SNKDataModel *)dataModel {
    _dataModel = dataModel;
    
    [self.imgIcon sd_setImageWithURL:[NSURL URLWithString:self.dataModel.imgsrc] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    self.lblTitle.text = self.dataModel.title;
    
    // 处理时间
    NSString *ptimeString = self.dataModel.ptime;
    NSString *fmt = @"YYYY/MM/dd HH:mm:ss";
    NSDate *timeAgoDate = [NSDate dateWithString:ptimeString formatString:fmt];
    self.resorceL.text = timeAgoDate.timeAgoSinceNow;
    
    // 如果回复太多就改成几点几万
    CGFloat count =  [self.dataModel.replyCount intValue];
    NSString *displayCount;
    if (count > 10000) {
        displayCount = [NSString stringWithFormat:@"%.1f万跟帖",count/10000];
    }else{
        displayCount = [NSString stringWithFormat:@"%.0f跟帖",count];
    }
    self.lblReply.text = displayCount;
    
    self.lblReply.kdj_width = [self.lblReply.text sizeWithFont:[UIFont systemFontOfSize:10] maxSize:CGSizeMake(200, MAXFLOAT)].size.width;
    self.lblReply.kdj_width += 10;
    self.lblReply.kdj_x = SCREENW - 10 - self.lblReply.kdj_width;
    
    [self.lblReply.layer setBorderWidth:1];
    [self.lblReply.layer setBorderColor:[UIColor darkGrayColor].CGColor];
    [self.lblReply.layer setCornerRadius:5];
    self.lblReply.clipsToBounds = YES;
}

#pragma mark - 返回可重用ID
+ (NSString *)idForRow:(SNKDataModel *)NewsModel
{
    if (NewsModel.hasHead && NewsModel.photosetID) {
        return @"kTopImageCell";  // Test 的 cell
    }else if (NewsModel.hasHead){
        return @"kTopTxtCell";
    }else if (NewsModel.imgType){
        return @"kBigImageCell";
    }else if (NewsModel.imgextra){
        return @"kImagesCell";    // 显示3张图的cell,但能否push,就看是否有Photoset
    }else{
        return @"kNewsCell";      // 标准的cell
    }
}

#pragma mark - 返回行高
+ (CGFloat)heightForRow:(SNKDataModel *)NewsModel
{
    if (NewsModel.hasHead && NewsModel.photosetID){
        return 0;
    }else if(NewsModel.hasHead) {
        return 0;
    }else if(NewsModel.imgType) {
        if (SCREENW == 320) {
            return 180;
        }else{
            return 196;
        }
    }else if (NewsModel.imgextra){
        if (SCREENW == 320) {
            return 135;
        }else{
            return 150;
        }
    }else{
        return 80;
    }
}

- (void)setFrame:(CGRect)frame {
    
    frame.size.height -= 1;
    [super setFrame:frame];
}

@end
